package com.hand.springbootdemo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hand.springbootdemo.common.Result;
import com.hand.springbootdemo.domain.entity.Student;
import com.hand.springbootdemo.domain.mapper.StudentMapper;
import com.hand.springbootdemo.dto.StudentVo;
import org.apache.catalina.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 13278
 */
@RestController
@RequestMapping("/hand")
public class StudentController {
    @Resource
    private StudentMapper studentMapper;

    @PostMapping("/student/add")
    public Result<Student> insertStudent(@RequestBody Student student) {
        studentMapper.insertStudent(student);
        return Result.ok(student);
    }

    @GetMapping("/student/{id}")
    public Result<Student> getStudent(@PathVariable("id") Long id) {
        Student student = studentMapper.selectById(id);
        return Result.ok(student);
    }

    @GetMapping("/search/{name}")
    public Result<List<Student>> searchStudent(@PathVariable(value = "name", required = false) String name) {
        List<Student> students = studentMapper.selectByName(name);
        return Result.ok(students);
    }

    @GetMapping("/search")
    public Result<List<Student>> searchStudent() {
        List<Student> students = studentMapper.selectByName(null);
        return Result.ok(students);
    }

    @GetMapping("/student/all")
    public Result<List<Student>> studentAll() {
        List<Student> students = studentMapper.selectAll();
        return Result.ok(students);
    }

    @DeleteMapping("/delete/{id}")
    public Result<Integer> deleteStudent(@PathVariable("id") Long id) {
        int flag = studentMapper.deleteById(id);
        return Result.ok(flag);
    }

    @PutMapping("/update")
    public Result<Student> updateStudent(@RequestBody Student student) {
        Integer i = studentMapper.updateStudent(student);
        Student student1 = studentMapper.selectById(student.getId());
        return Result.ok(student1);
    }

    @GetMapping("/score-table/{id}")
    public Result<StudentVo> getScoreTable(@PathVariable(value = "id") Long id) {
        StudentVo studentVo = studentMapper.selectStudentScoreTableById(id);
        return Result.ok(studentVo);
    }

    @GetMapping("/score-table/all")
    public Result<List<StudentVo>> getScoreTableAll() {
        List<StudentVo> studentVos = studentMapper.selectStudentScoreTableAll();
        return Result.ok(studentVos);
    }

    @PostMapping("/student/add-score")
    public Result<StudentVo> addScore(@RequestBody Map<String, Number> scoreMap) {
        int i = studentMapper.insertScore(scoreMap.get("studentId").longValue(), scoreMap.get("courseId").longValue(), scoreMap.get("score").doubleValue());
        return getScoreTable(scoreMap.get("studentId").longValue());
    }

    @PostMapping("/student/add-score-beach")
    public Result<List<StudentVo>> addScoreBeach(@RequestBody List<Map<String, Object>> scores) {
        System.out.println(scores);
        int i = studentMapper.insertScoreBeach(scores);
        return getScoreTableAll();
    }
}
